﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace FirstFloor.Reversi.Controls
{
    /// <summary>
    /// The base class for all FirstFloor controls
    /// </summary>
    public abstract class ControlBase
        : Control
    {
        /// <summary>
        /// Occurs when the mouse leaves the root object.
        /// </summary>
        public event EventHandler RootLeave;

        private static Dictionary<string, string> XamlCache = new Dictionary<string,string>();

        private FrameworkElement control = null;

        /// <summary>
        /// Initializes a new instance of the ControlBase class.
        /// </summary>
        public ControlBase()
        {
            Type type = GetType();
            string cacheKey = type.FullName;

            string xaml;
            if (!XamlCache.TryGetValue(cacheKey, out xaml)) {
                Assembly assembly = type.Assembly;

                Stream stream = assembly.GetManifestResourceStream(XamlResourceName);
                
                Debug.Assert(stream != null, string.Format("The resource template {0} could not be found", XamlResourceName));

                using(stream){
                    using(StreamReader reader = new StreamReader(stream)){
                        xaml = reader.ReadToEnd();
                    }
                }

                XamlCache[cacheKey] = xaml;
            }

            this.control = InitializeFromXaml(xaml);

            Debug.Assert(this.control != null, "Failed to initialize the control");

            base.Width = this.control.Width;
            base.Height = this.control.Height;

            Loaded += new EventHandler(OnLoaded);
        }

        /// <summary>
        /// Find the object having given name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public new DependencyObject FindName(string name)
        {
            return this.control.FindName(name);
        }

        /// <summary>
        /// Gets or sets the width of the control.
        /// </summary>
        public new double Width
        {
            get { return this.control.Width; }
            set
            {
                base.Width = this.control.Width = value;
                UpdateLayout();
            }
        }

        /// <summary>
        /// Gets or sets the height of the control.
        /// </summary>
        public virtual new double Height
        {
            get { return this.control.Height; }
            set
            {
                base.Height = this.control.Height = value;
                UpdateLayout();
            }
        }

        /// <summary>
        /// The resource name containg the xaml used to initialize the actual object
        /// </summary>
        protected abstract string XamlResourceName { get; }

        /// <summary>
        /// Gets a reference to the actual control.
        /// </summary>
        protected FrameworkElement Control
        {
            get { return this.control; }
        }

        /// <summary>
        /// Finds the root and registers for its MouseLeave
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        protected virtual void OnLoaded(object sender, EventArgs args)
        {
            FrameworkElement root = this;
            do {
                root = root.Parent as FrameworkElement;
            } while (root != null && root.Parent != null);

            if (root != null) {
                root.MouseLeave += new EventHandler(RaiseRootLeave);
            }
            Height = base.Height;
            Width = base.Width;
        }

        /// <summary>
        /// Called when the element is loaded or Width or Height is changed. The base implementation does nothing.
        /// </summary>
        protected virtual void UpdateLayout()
        {
        }

        /// <summary>
        /// Raises the RootLeave event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void RaiseRootLeave(object sender, EventArgs args)
        {
            if (RootLeave != null) {
                RootLeave(sender, args);
            }
        }
    }
}
